package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.RepertoireDao;
import com.hzdl.cinema.domain.Play;
import com.hzdl.cinema.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RepertoireDaoImpl implements RepertoireDao {
            JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int totalRepertoire(String select_button) {
        if(select_button!=null){
            if(select_button.equals("")||select_button.equals("null")){
                select_button=null;
            }
        }

        String sql="select count(*) from play where 1=1 and if (? is null ,1=1,play_Name like concat('%',?,'%')) ";
        Integer integer_totalRepertoire = jdbcTemplate.queryForObject(sql, Integer.class,select_button,select_button);
        return integer_totalRepertoire;
    }

    @Override
    public List<Play> fiedRepertoire(int index, Integer rows,String select_button) {
        if(select_button!=null){
            if(select_button.equals("")||select_button.equals("null")){
                select_button=null;
            }
        }
        String sql="select * from play where 1=1 and if(? is null,1=1,play_Name like concat('%',?,'%'))  limit ?,?";
        List<Play> playList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Play.class),select_button,select_button ,index, rows);

        return playList;
    }

    @Override
    public void addPlay(Play play) {
        String sql="insert into play values(null,?,?,?,?,?)";
        jdbcTemplate.update(sql,play.getPlayName(),play.getPlayDescription(),play.getPlayLength(),play.getPlayImage(),play.getPlayType());
    }

    @Override
    public void deletePlay(String id) {
        String sql ="delete from play where play_Id=? ";
        jdbcTemplate.update(sql,id);

    }

    @Override
    public Play playEcho(String id) {
        String sql="select * from play where play_Id=?";
        Play play = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Play.class), id);
        return play;
    }

    @Override
    public void playUpdete(Play play) {
        String sql = "update play  set  play_Name= ? , play_Length= ? , play_Type= ? , play_Image= ? , play_Description= ?  where play_Id = ?";
        jdbcTemplate.update(sql,play.getPlayName(),play.getPlayLength(),play.getPlayType(),play.getPlayImage(),play.getPlayDescription(),play.getPlayId());
    }


    @Test
    public  void  mothed(){
        int i = totalRepertoire("虫");
        System.out.println(i);
        List<Play> list = fiedRepertoire(0, 4,"虫");
        System.out.println(list);
    }
    @Test
    public  void  mothed02(){
        Play play=new Play();
        play.setPlayName("qq");
        addPlay(play);
    }
    @Test
    public  void  mothed03(){
//        Play play=new Play();
//        play.setPlayName("qq");
//        addPlay("1",2,"2","e","f");
        Play play=new Play();
        play.setPlayName("qq");
        play.setPlayId(51);
        playUpdete(play);
    }
}
